<?php
/**
 * Специфическая подготовка данных для типа документа "Голосование"
 *
 * @package default
 * @author Alexander Sibiryakov
 **/
class AggregatedModel_ItemProcessor_VoteType extends AggregatedModel_ItemProcessor_Common implements AggregatedModel_ItemProcessor_Interface
{

	public function processItem(array $data) {

		// если запись это голосование, то распарсим варианты ответа и установим кол-во голосующих
		if ($data['type'] == 'fvote') {
			$cdate_till = RL_CyrDate::newFromStr("%Y-%m-%d", $data['date_till']);

			if ($cdate_till && ($cdate_till->unix_mark() <= RL_CyrDate::newNow()->unix_mark()) )
				$data['vote_closed'] = true;
			else $data['vote_closed'] = false;

			$answers = $data['answers'];
			if (isset($data['votes'])) $votes = $data['votes']; else
				$votes = array();

			$max_voters = 0;
			$overall_voters = 0;
			foreach ($answers as $i=>$answer) {

				if (isset($votes[$i])) {
					$answer['voters'] = $votes[$i];
					if ($votes[$i] > $max_voters) $max_voters = $votes[$i];
					$overall_voters += $votes[$i];
				} else $answer['voters'] = 0;

				$answers[$i] = $answer;
			}

			$data['answers'] = $answers;
			$data['max_voters'] = $max_voters;
			$data['overall_voters'] = $overall_voters;
		}

		return $data;
	}
} // END class
